package com.xhh.nphm.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.xhh.nphm.common.core.domain.QueryConditionVo;
import org.apache.poi.ss.formula.functions.T;

import java.util.*;
import java.util.Map.Entry;
import javax.servlet.http.HttpServletRequest;

/**
 * Map通用处理方法
 *
 * @author ydq
 */
public class MapDataUtil {
    public static Map<String, Object> convertDataMap(HttpServletRequest request) {
        Map<String, String[]> properties = request.getParameterMap();
        Map<String, Object> returnMap = new HashMap<String, Object>();
        Iterator<?> entries = properties.entrySet().iterator();
        Entry<?, ?> entry;
        String name = "";
        String value = "";
        while (entries.hasNext()) {
            entry = (Entry<?, ?>) entries.next();
            name = (String) entry.getKey();
            Object valueObj = entry.getValue();
            if (null == valueObj) {
                value = "";
            } else if (valueObj instanceof String[]) {
                String[] values = (String[]) valueObj;
                value = "";
                for (int i = 0; i < values.length; i++) {
                    value += values[i] + ",";
                }
                if (value.length() > 0) {
                    value = value.substring(0, value.length() - 1);
                }
            } else {
                value = valueObj.toString();
            }
            returnMap.put(name, value);
        }
        return returnMap;
    }

    /**
     * 参数封装为map
     *
     * @param queryConditionVo  实体参数
     * @param otherParam        路径参数
     * @return
     */
    public static Map<String,Object> multiTypeParamListToMap(QueryConditionVo queryConditionVo, String... otherParam){
        Map<String, Object> map = new HashMap<>();
        if(StringUtils.isNotNull(queryConditionVo)){
            map = JSONObject.parseObject(JSONObject.toJSONString(queryConditionVo)).getInnerMap();
        }
        if(StringUtils.isNotNull(otherParam)){
            for (int i = 0, j = 1; i < otherParam.length; i++,j++) {
                map.put("param"+j,otherParam[i]);
            }
        }
        return map;
    }

    /**
     * 参数封装为map
     *
     * @param paramEntity  实体参数
     * @param otherParam        路径参数
     * @return
     */
    public static Map<String,Object> multiEntityParamListToMap(Object paramEntity, String... otherParam){
        Map<String, Object> map = new HashMap<>();
        if(StringUtils.isNotNull(paramEntity)){
            map = JSONObject.parseObject(JSONObject.toJSONString(paramEntity)).getInnerMap();
        }
        if(StringUtils.isNotNull(otherParam)){
            for (int i = 0, j = 1; i < otherParam.length; i++,j++) {
                map.put("param"+j,otherParam[i]);
            }
        }
        return map;
    }

}
